package biz.datalk.industrialland.admin.modules.online.dynform.controller;

import biz.datalk.industrialland.admin.modules.common.servie.ICommonService;
import biz.datalk.industrialland.admin.modules.online.config.util.DynfromButtonUtil;
import biz.datalk.industrialland.admin.modules.online.dynform.pojo.dto.DictItem;
import biz.datalk.industrialland.admin.modules.online.dynform.pojo.dto.DynformAutoDTO;
import biz.datalk.industrialland.admin.modules.online.dynform.pojo.dto.DynformFieldDTO;
import biz.datalk.industrialland.admin.modules.online.dynform.pojo.po.DynformButton;
import biz.datalk.industrialland.admin.modules.online.dynform.pojo.po.DynformHead;
import biz.datalk.industrialland.admin.modules.online.dynform.service.IDynformAutoService;
import biz.datalk.industrialland.admin.modules.online.dynform.service.IDynformButtonService;
import biz.datalk.industrialland.admin.modules.online.dynform.service.IDynformEnhanceJsService;
import biz.datalk.industrialland.admin.modules.online.dynform.service.IDynformServices;
import biz.datalk.industrialland.common.def.enums.DBYNEnum;
import biz.datalk.industrialland.common.encrypt.annotation.RSAES;
import biz.datalk.industrialland.common.result.UnifyResult;
import biz.datalk.industrialland.common.util.FieldUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author tarofang@163.com
 * @date 2019-12-19
 */
@RestController
@RequestMapping("/dynformAuto")
public class DynfromAutoController {

    private IDynformAutoService dynformAutoService;
    private IDynformServices dynformServices;
    private IDynformButtonService dynformButtonService;
    private IDynformEnhanceJsService dynformEnhanceJsService;

    @Autowired
    public void setDynformAutoService(IDynformAutoService dynformAutoService) {
        this.dynformAutoService = dynformAutoService;
    }
    @Autowired
    public void setDynformServices(IDynformServices dynformServices) {
        this.dynformServices = dynformServices;
    }
    @Autowired
    public void setDynformButtonService(IDynformButtonService dynformButtonService) {
        this.dynformButtonService = dynformButtonService;
    }
    @Autowired
    public void setDynformEnhanceJsService(IDynformEnhanceJsService dynformEnhanceJsService) {
        this.dynformEnhanceJsService = dynformEnhanceJsService;
    }

    @RequestMapping(value = "/index/{tableId}")
    public ModelAndView indexPage(@PathVariable(value = "tableId") String tableId) {
        if (StringUtils.isBlank(tableId)) {
            throw new IllegalArgumentException("invalid tId");
        }
        DynformHead table =  dynformServices.selectByPk(tableId);
        if (table == null) {
            throw new IllegalArgumentException("invalid tId");
        }
        List<DynformFieldDTO> dynformFieldDto = dynformServices.selectByTableId(table.getId(), Boolean.TRUE);

        // 列表显示字段
        List<DynformFieldDTO> listFields = new ArrayList<>();
        // 查询字段
        List<DynformFieldDTO> searchFields = new ArrayList<>();
        // 表单字段(添加、编辑)
        List<DynformFieldDTO> formFields = new ArrayList<>();
        for (DynformFieldDTO field : dynformFieldDto) {
            field.setFieldNameHump(FieldUtil.underline2Hump(field.getFieldName()));
            if (StringUtils.equals(field.getPageIsQuery(), DBYNEnum.Y.name())) {
                searchFields.add(field);
            }
            if (StringUtils.equals(field.getPageIsShowList(), DBYNEnum.Y.name())) {
                listFields.add(field);
            }
            if (StringUtils.equals(field.getPageIsShow(), DBYNEnum.Y.name())) {
                formFields.add(field);
            }

            // 字典信息
        }


        List<DynformButton> btnList = dynformButtonService.getButtons(tableId, "");
        Map<String, List<DynformButton>> useBtnsMap = DynfromButtonUtil.packageUseBtns(btnList, table.getTableDefBtns(), tableId);
        List<DynformButton> linkBtns = useBtnsMap.get(DynfromButtonUtil.BTN_STYLE_LINK);
        List<DynformButton> buttonBtns = useBtnsMap.get(DynfromButtonUtil.BTN_STYLE_BUTTON);

        ModelAndView modelAndView = new ModelAndView("biz/datalk/framework/core/dynform/auto/index");
        // 列表显示值
        modelAndView.addObject("columns", listFields);
        // 查询条件
        modelAndView.addObject("searchs", searchFields);
        // 表的属性
        modelAndView.addObject("table", table);
        // 表单属性
        modelAndView.addObject("forms", formFields);

        // link 按钮（表格类的操作按钮）
        modelAndView.addObject("linkBtns", linkBtns);
        // button 按钮 (表格上的按钮)
        modelAndView.addObject("buttonBtns", buttonBtns);

        return modelAndView;
    }

    @RSAES
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifyResult list(@RequestBody DynformAutoDTO dynformAutoDTO) {
        return dynformAutoService.list(dynformAutoDTO);
    }

    @RSAES
    @PostMapping(value = "/detail", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifyResult detail(@RequestBody DynformAutoDTO dynformAutoDTO) {
        return dynformAutoService.detail(dynformAutoDTO);
    }

    @RSAES
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifyResult add(@RequestBody DynformAutoDTO dynformAutoDTO) {
        return dynformAutoService.add(dynformAutoDTO);
    }

    @RSAES
    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifyResult edit(@RequestBody DynformAutoDTO dynformAutoDTO) {
        return dynformAutoService.edit(dynformAutoDTO);
    }

    @RSAES
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifyResult delete(@RequestBody DynformAutoDTO dynformAutoDTO) {
        return dynformAutoService.delete(dynformAutoDTO);
    }

    @RSAES
    @PostMapping(value = "/doButton", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifyResult doButton(@RequestBody DynformAutoDTO dynformAutoDTO) {
        return dynformAutoService.doButton(dynformAutoDTO);
    }

    @RSAES
    @PostMapping(value = "/enhanceJs", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifyResult enhanceJs(@RequestBody DynformAutoDTO dynformAutoDTO) {
        return dynformAutoService.enhanceJs(dynformAutoDTO);
    }


    @Autowired private ICommonService commonService;

    @PostMapping(value = "/mytest", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifyResult mytest(HttpServletRequest request) {

        String dictTable = "sys_dict_item WHERE dict_id='888f522510e9485a8cb0a83ce973c514'";
        String dictCode = "item_value";
        String dictText = "item_text";


        // List<DictItem> dictItems = commonService.selectDictFromCustomTable(dictTable, dictCode, dictText);

        dictCode = "dynform_page_show_type";
        List<DictItem> dictItems = commonService.selectDictFromSysdict(dictCode);
        return UnifyResult.success().setData(dictItems);
    }

}
